package org.example.service;

import org.example.entity.Dds;
import org.example.entity.Dingdan;
import org.example.entity.Sjtj;
import org.example.entity.Tq;
import org.example.mapper.Dingdanmapper;
import org.example.mapper.Shanpinfelemapper;
import org.example.mapper.Shanpinmapper;
import org.example.vo.DingdanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class DingdanService {
    @Autowired
    Dingdanmapper dingdanmapper;
    @Autowired
    PinjiaService pinjiaService;
    @Autowired
    private Shanpinmapper shanpinmapper;
    @Autowired
    private Shanpinfelemapper shanpinfelemapper;

    /**
     * 新增
     * @param dingdan
     */
    public void xz(Dingdan dingdan) {
        dingdan.setTime(LocalDateTime.now());
        dingdanmapper.xz(dingdan);
    }

    /**
     * 查询
     * @param username
     * @return
     */
    public List<Dingdan> cx(String username ,int zt) {
        List<Dingdan> dingdanList=dingdanmapper.cx(username,zt);
        if(zt==4){
            for(Dingdan dingdan:dingdanList){
               dingdan.setPj(pinjiaService.cx( dingdan.getId()));
            }

        }
       return  dingdanList;
    }

    /**
     * 修改
     * @param id
     */
    public void xg(int id,int zt) {
        dingdanmapper.xg(id,zt);
    }

    /**
     * 删除
     * @param id
     */
    public void sc(int id) {

        dingdanmapper.sc(id);
    }

    /**
     * 退款修改
     * @param id
     * @param i
     * @param tuih
     */
    public void xgxz(int id, int i, String tuih) {
        dingdanmapper.xgxz(id,i,tuih);
    }

    /**
     * 数目查询
     * @param username
     * @return
     */
    public List<Integer> shumcx(String username) {
        List<Integer> integers=new ArrayList<>();
        integers.add(dingdanmapper.shucx(username,0));
        integers.add(dingdanmapper.shucx(username,1));
        integers.add(dingdanmapper.shucx(username,2));
        integers.add(dingdanmapper.shucx(username,3));
        return  integers;
    }

    /**
     * 商家处理查询
     * @param username
     * @param i
     * @return
     */
    public List<Dingdan> glcx(String username, int i) {
        List<Dingdan> dingdanList=dingdanmapper.glcx(username,i);

            for(Dingdan dingdan:dingdanList){
                dingdan.setPj(pinjiaService.cx( dingdan.getId()));
            }


        return dingdanList;
    }

    /**
     * 注销查询
     * @param username
     * @return
     */
    public int zxcx(String username) {
        int i=dingdanmapper.shucx(username,1)+dingdanmapper.shucx(username,2)+dingdanmapper.shucx(username,5);
        return i;
    }

    public void clsc(int id,int i) {
        dingdanmapper.clxg(id,i);
        dingdanmapper.clsc(id);
    }

    /**
     * 数据统计
     * @param username
     * @return
     */
    public List<DingdanVO> sjtjcx(String username) {

        List<DingdanVO> dingdanVOS = dingdanmapper.sjtjcx(username);

        for (int i = dingdanVOS.size() - 1; i >= 0; i--) {
            DingdanVO dingdanVO = dingdanVOS.get(i);
            if (pinjiaService.cx(dingdanVO.getId()) != null) {

            } else {
                if (dingdanVO.getTuih()!=null&&!Objects.equals(dingdanVO.getTuih(), "") ) {
                    dingdanVOS.remove(i); // 直接通过索引删除
                }
            }
        }

        return dingdanVOS;
    }

    /**
     * 统计查询
     * @param username
     * @return
     */
    public Sjtj tjcx(String username) {
        // 获取当前日期时间
        LocalDateTime now = LocalDateTime.now();
        // 获取今天最早的时间
        LocalDateTime todayStart = now.toLocalDate().atStartOfDay();
        // 获取今天最晚的时间（23:59:59）
        LocalDateTime todayEnd = now.toLocalDate().atTime(23, 59, 59);
        List<DingdanVO> dingdanList=dingdanmapper.tjcx(username,todayStart,todayEnd );
        // 使用一个集合来记录已经出现过的 username
        Set<DingdanVO> seenUsernames = dingdanmapper.ctjcx(username,todayStart,todayEnd );

        int ye=0;
        int youdd=0;
        int wudd=0;
        for(DingdanVO dingdanVO:dingdanList){
            if(pinjiaService.cx(dingdanVO.getId())!=null){
                ye+=dingdanVO.getPrice()+dingdanVO.getYingfe();
                youdd++;
            }else {
                if (dingdanVO.getTuih()!=null&&!Objects.equals(dingdanVO.getTuih(), "") ) {

                    wudd++;
                }else {
                    ye += dingdanVO.getPrice() + dingdanVO.getYingfe();
                    youdd++;
                }
            }

        }
        Sjtj sjtj=new Sjtj();
        sjtj.setYe(ye);
        sjtj.setYouxdd(youdd);
        if((youdd+wudd)==0) {
            sjtj.setDdwcl(0);
        }else{
            double s = (100.0 * youdd) / (youdd + wudd);
            int roundedS = (int) Math.round(s); // 四舍五入
            sjtj.setDdwcl(roundedS);

        }
        if(youdd==0){
            sjtj.setPjdd(0);
        }else {
            sjtj.setPjdd(ye / youdd);
        }
        sjtj.setYonhu(seenUsernames.toArray().length);
        return sjtj;
    }

    /**
     * 订单数
     * @param username
     * @return
     */
    public Dds ddscx(String username) {
        Dds dds=new Dds();
        int fh=0;
        int th=0;
        int wc=0;
        int qx=0;
        int sy=0;
        List<Dingdan> dingdanVOS = dingdanmapper.sycx(username);

        for (int i = dingdanVOS.size() - 1; i >= 0; i--) {
            Dingdan dingdanVO = dingdanVOS.get(i);
            sy++;
            if(dingdanVO.getZt()==1){
                fh++;
                continue;
            }
            if(dingdanVO.getZt()==5){
                th++;
                continue;
            }
            if (pinjiaService.cx(dingdanVO.getId()) != null) {

                wc++;
            } else {
                if (dingdanVO.getTuih()!=null&&!Objects.equals(dingdanVO.getTuih(), "") ) {
                    qx++;
                }else{
                    wc++;
                }
            }
        }
        dds.setFh(fh);
        dds.setSy(sy);
        dds.setQx(qx);
        dds.setWc(wc);
        dds.setTh(th);
        return dds;
    }

    /**
     * 停启
     * @param username
     * @return
     */
    public Tq tqcx(String username) {
        Tq tq=new Tq();
        tq.setQ1(shanpinmapper.shucx(username));
        tq.setT1(shanpinmapper.shutcx(username));
        tq.setQ2(shanpinfelemapper.shuqcx(username));
        tq.setT2(shanpinfelemapper.shutcx(username));
        return tq;
    }
}
